import MySQLdb, os, errno
def mkdir_p(path):
    try:
        os.makedirs(path)
    except OSError as exc: # Python >2.5
        if exc.errno == errno.EEXIST:
            pass
        else: raise
        
rootdir = "/tmp/sqlfslog"    
mkdir_p(rootdir)
#db = MySQLdb.connect("localhost", "root", "1234", "sqlfs", 3306);
db = MySQLdb.connect("localhost", "root", "1234", "sqlfs", 4040);
c = db.cursor();
c.execute("SHOW TABLES");
tables = [row[0] for row in c.fetchall()]
for table in tables:
    filename = "%s/%s.csv" % (rootdir,table)    
    f = open(filename, 'w')
    c.execute("SELECT * FROM %s ORDER BY rowid" % table)
    while 1:
        row = c.fetchone()
        if row == None:
            break
        print >>f, ','.join(map(lambda x:'' if x is None else str(x), row[1:]))
    f.close()    
    